package sse.ustc.cc.custom;

import sse.ustc.cc.activity.R;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;
import android.widget.TextView;
/**
 * 选择课程周数dialog
 * @author xuhao
 *
 */
public class WeekChooseDialog extends Dialog{

	private TextView startWeekPlay = null;//开始周数
	private TextView endWeekPlay = null;//结束周数
	
	private NumberPicker startWeek = null;
	private NumberPicker endWeek = null;
	
	private int startWeekNum = 1;
	private int endWeekNum = 1;
	
	Context context;
	
	public WeekChooseDialog(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public WeekChooseDialog(Context context, int theme){
        super(context, theme);
        this.context = context;
    }
	
	/** Called when the activity is first created. */
    @SuppressLint("NewApi") @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //点击对话框外禁止取消窗口
        setCanceledOnTouchOutside(false);
        setContentView(R.layout.weekchoose);
        
        startWeekPlay = (TextView)findViewById(R.id.start_weekplay);
        endWeekPlay = (TextView)findViewById(R.id.end_weekplay);
        //初始化周数显示
        startWeekPlay.setText(startWeekNum+"");
        endWeekPlay.setText(endWeekNum+"");
        
        startWeek = (NumberPicker)findViewById(R.id.start_week);
        endWeek = (NumberPicker)findViewById(R.id.end_week);
        
        //设置startWeek的最小值和最大值
        startWeek.setMinValue(1);
        startWeek.setMaxValue(25);
        //设置endWeek的最小值和最大值
        endWeek.setMinValue(1);
        endWeek.setMaxValue(25);
        //设置startWeek的当前值
        startWeek.setValue(1);
        //设置endWeek的当前值
        endWeek.setValue(1);
        //开始周数选择响应事件
        startWeek.setOnValueChangedListener(new OnValueChangeListener(){

            @Override
            public void onValueChange(NumberPicker picker, int oldVal,
                    int newVal) {
                // TODO Auto-generated method stub
            	startWeekPlay.setText(newVal+"");
            	setStartWeekNum(newVal);
            }
            
        });
        //结束周数选择响应事件
        endWeek.setOnValueChangedListener(new OnValueChangeListener(){

            @Override
            public void onValueChange(NumberPicker picker, int oldVal,
                    int newVal) {
                // TODO Auto-generated method stub
            	if(newVal<startWeekNum){
            		endWeekPlay.setError("结束周数要大于开始周数。");
            		endWeekPlay.setText(startWeekNum+"");
                	setEndWeekNum(startWeekNum);
            		return;
            	}
            	endWeekPlay.setText(newVal+"");
            	setEndWeekNum(newVal);
            }
            
        });
        
        
    }
    

	public int getStartWeekNum() {
		return startWeekNum;
	}

	public void setStartWeekNum(int startWeekNum) {
		this.startWeekNum = startWeekNum;
	}

	public int getEndWeekNum() {
		return endWeekNum;
	}

	public void setEndWeekNum(int endWeekNum) {
		this.endWeekNum = endWeekNum;
	}
}
